package com.tledu.production.controller.plan;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.plan.OrderManagement;
import com.tledu.production.model.plan.ProductManagement;
import com.tledu.production.service.plan.PlanService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/OrderController")
public class OrderController {
    private final PlanService planService;
    @GetMapping("/plan/OrderManagement")
    public String getAllO(Model model,Integer pageNum, Integer pageSize ){
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 4;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<OrderManagement> order=planService.getAllO();
        PageInfo<OrderManagement> page = PageInfo.of(order);//把获取到的数据封装到page中
        model.addAttribute("getAllO",page.getList());
        model.addAttribute("page", page);
        model.addAttribute("pages",new int[page.getPages()]);
        return "plan/OrderManagement";
    }
    @GetMapping("/plan/order-add")
    public String addO(){
        return "plan/add/order-add";
    }

    @PostMapping("/plan/order-add")
    @ResponseBody
    public AjaxResult<Void> addO(OrderManagement orderManagement){
    planService.insertO(orderManagement);
    return new AjaxResult<>();
    }
//    ***************编辑*********************
    @GetMapping("/plan/edit/order-edit/updateO/{orderId}")
    public String selectO(Model model, @PathVariable Integer orderId){
        OrderManagement orderManagement=planService.selectO(orderId);
        model.addAttribute("orderManagement",orderManagement);
        return "plan/edit/order-edit";
    }
    @PostMapping("/plan/edit/order-edit/updateO")
    @ResponseBody
    public AjaxResult<Void> updateO(OrderManagement orderManagement){
        planService.updateO(orderManagement);
        return new AjaxResult<>();
    }
    @DeleteMapping("/depBatchDeleteO")
    @ResponseBody
    public AjaxResult<OrderManagement> batchDelete(String idList){
        String[] array=idList.split(",");
        for (String s : array){
//            planService.deleteO(s);
        }
        return new AjaxResult<>("删除成功");
    }
    //删除
    @GetMapping("/plan/OrderManagement/deleteO")
    @ResponseBody
    public JSONObject deleteO(String id){
        int delete= planService.deleteO(id);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("success",delete);
        return jsonObject;
    }
    //富文本
    @GetMapping("/richTextO/{orderId}")
    public String richTextO(Model model,  @PathVariable Integer orderId){
        OrderManagement orderManagement=planService.selectO(orderId);
        model.addAttribute("orderManagement",orderManagement);
      return "plan/richText/OrderRichText";
    }


    //搜索
    @GetMapping("/plan/OrderManagement/search")
    public String search(Model model,Integer pageNum, Integer pageSize,String type,String searchText){
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 4;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<OrderManagement> order=planService.searchO(type, searchText);
        PageInfo<OrderManagement> page = PageInfo.of(order);//把获取到的数据封装到page中
        model.addAttribute("getAllO",page.getList());
        model.addAttribute("page", page);
        model.addAttribute("pages",new int[page.getPages()]);
        return "plan/OrderManagement";
    }

}
